package com.hlx.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlx.entity.user.TrainNumberStation;
import com.hlx.service.admin.TrainNumberService;
import com.hlx.service.user.TrainNumberStationService;
import com.hlx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/trainstation")
public class TrainNumberStationController {

    @Autowired
    private TrainNumberStationService trainStationService;
    @Autowired
    private TrainNumberService trainNumberService;

    //查询列车经过的站
    @GetMapping("/getByNumber/{trainNumberName}/{date}")
    public Result<List<TrainNumberStation>> selectTrainByNumber(@PathVariable("trainNumberName") String trainNumberName,@PathVariable("date") Date date){
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        if(!trainNumberService.exitTrainData(trainNumberName,localDate)){
            return Result.error("这个时间段内无此车");
        }
        LambdaQueryWrapper<TrainNumberStation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainNumberStation::getTrainNumberName,trainNumberName);
        List<TrainNumberStation> list = trainStationService.list(queryWrapper);
        System.out.println(list);
        if(list.isEmpty()){
            return Result.error("查无此序号列车");
        }
        return Result.success(list);
    }

//    @PostMapping("/getByTwoName")
//    public Result<List<TrainNumberStation>> selectTrainByTwoName(@RequestBody List<String> stationName){
//        LambdaQueryWrapper<TrainNumberStation> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(TrainNumberStation::getStationName,stationName);
//        queryWrapper.groupBy(TrainNumberStation::getTrainNumberName);
//        queryWrapper.having("COUNT(train_number_name) = 2");
//        List<TrainNumberStation> tns = trainStationService.list(queryWrapper);
//        System.out.println(tns);
//        return Result.success(tns);
//    }
}
